﻿using System;
using System.Collections.Generic;
using System.Linq;
using LDSCore;
using LDSCore.Result;

namespace LDSModel.Dal
{
    public class DeviceService
    {
        static readonly object ObjLock = new object();

        /// <summary>
        /// 油罐列表
        /// </summary>
        public List<Data_OilTank> OilTankList { get; private set; }

        /// <summary>
        /// 设备列表
        /// </summary>
        public List<Data_Device> DeviceList { get; private set; }

        /// <summary>
        /// 刷新内存数据
        /// </summary>
        public void Refush()
        {
            if (OilTankList != null)
            {
                OilTankList.Clear();
                OilTankList = null;
            }
            if (DeviceList != null)
            {
                DeviceList.Clear();
                DeviceList = null;
            }
            OilTankList = LoadOilTankList();
            DeviceList = LoadAllDeviceList();
        }

        public OperationResult UpdateAlarmValue(Guid deviceId, decimal value)
        {
            var result = new OperationResult();
            var context = new DatabaseEntities();

            var device = (from d in context.Data_Device where d.DeviceId == deviceId select d).FirstOrDefault();

            var minValue = new decimal(0.0f);
            const int maxValue = 100;

            if (device != null)
                switch (device.Position)
                {
                    case 1:
                        {
                        }
                        break;
                    default:
                        {
                        }
                        break;
                }

            if (value < minValue || value > maxValue)
            {
                result.Success = false;
                result.Exception = new Exception("请输入" + minValue + "-" + maxValue + "之间的数字!");
                return result;
            }

            if (device != null) device.AlarmValue = value;
            context.SaveChanges();

            lock (DeviceList)
            {
                DeviceList = LoadAllDeviceList();
            }

            result.Success = true;

            return result;
        }

        public decimal GetAlermValue(Guid deviceId)
        {
            lock (DeviceList)
            {
                var device = (from d in DeviceList where d.DeviceId == deviceId select d).FirstOrDefault();
                if (device != null)
                    if (device.AlarmValue != null) return device.AlarmValue.Value;
            }
            return 0;
        }

        #region 从数据库获取数据

        /// <summary>
        /// 获取油罐列表
        /// </summary>
        /// <returns></returns>
        List<Data_OilTank> LoadOilTankList()
        {
            var context = new DatabaseEntities();

            var list = from o in context.Data_OilTank
                       where o.State == true
                       orderby o.Name
                       select o;

            //oList.Sort(delegate(Data_OilTank a, Data_OilTank b) { return (a.Left + a.Top) > (b.Left + b.Top) ? 0 : 1; });
            return Enumerable.ToList(list);
        }



        /// <summary>
        /// 获取设备列表
        /// </summary>
        /// <returns></returns>
        List<Data_Device> LoadAllDeviceList()
        {
            var context = new DatabaseEntities();

            var list = from d in context.Data_Device
                       orderby d.Address, d.Position
                       select d;

            return Enumerable.ToList(list);
        }

        /// <summary>
        /// 得到丢失的记录
        /// </summary>
        /// <param name="deviceId">设备Id</param>
        /// <param name="count">记录条数</param>
        /// <returns></returns>
        public IQueryable<long> GetLostRecord(Guid deviceId, int count)
        {
            var context = new DatabaseEntities();

            var result = from p in context.Data_Records
                         where p.DeviceID == deviceId
                               && !(from d in context.Data_Records
                                    where d.DeviceID == deviceId
                                    select d.AlarmCount).Contains(p.AlarmCount - 1)
                               && (p.AlarmCount - 1) != 0
                         orderby p.AlarmCount descending
                         select (p.AlarmCount.Value - 1);

            if (count > 0)
                result = result.Take(count);

            return result;
        }

        #endregion

        #region 从内存获取数据

        public Data_OilTank GetOilTank(Guid oid)
        {
            return (from o in OilTankList
                    where o.OId == oid
                    select o).FirstOrDefault();
        }

        public Data_OilTank GetOilTankByDeviceId(Guid deviceId)
        {
            return (from o in OilTankList
                    from d in DeviceList
                    where o.OId == d.OID && d.DeviceId == deviceId
                    select o).FirstOrDefault();
        }

        /// <summary>
        /// 获得指定的设备
        /// </summary>
        /// <param name="deviceId">设备Id</param>
        /// <returns>返回指定的设备</returns>
        public Data_Device GetDevice(Guid deviceId)
        {
            return (from d in DeviceList
                    where d.DeviceId == deviceId
                    select d).FirstOrDefault();
        }

        public string GetDeviceNameByAddress(int address)
        {
            return (from o in OilTankList
                    from d in DeviceList
                    where o.OId == d.OID && d.Address == address
                    select o.Name + d.Name).FirstOrDefault();
        }

        public Data_Device GetDevice(int address)
        {
            return (from d in DeviceList
                    where d.Address == address
                    select d).FirstOrDefault();
        }

        /// <summary>
        /// 获取设备列表
        /// </summary>
        /// <returns></returns>
        public List<Data_Device> LoadDeviceList(Guid oid)
        {
            var list = from d in DeviceList
                       where d.OID == oid
                       orderby d.Position, d.Name
                       select d;

            return Enumerable.ToList(list);
        }

        #endregion

        static DeviceService _instance;
        /// <summary>
        /// 单例
        /// </summary>
        public static DeviceService Instance
        {
            get
            {
                lock (ObjLock)
                {
                    return _instance ?? (_instance = new DeviceService());
                }
            }
        }
    }


    //public class Data_OilTank_Compare : IComparable<Data_OilTank>
    //{

    //}
}
